package com.audiocn.dc;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;

import com.audiocn.manager.BaseManager;
import com.audiocn.poster.Application;

public abstract class BaseDC extends FrameLayout implements OnClickListener{
	public BaseManager manager;
	public Context context;
	public int ScreenWidth;
	public int ScreenHeight;
	boolean mIsShown=false;
	InputMethodManager inputMethodManager;
	long l=0;
	public BaseDC(Context context,int layoutId,BaseManager manager) {
		super(context);
		this.context=context;
		this.manager=manager;
		if(layoutId!=-1){
			inflate(context, layoutId, this);  
		}
		WindowManager wm=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		ScreenHeight=wm.getDefaultDisplay().getHeight();
		ScreenWidth=wm.getDefaultDisplay().getWidth();
		inputMethodManager = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
	}
	
	public BaseDC(Context context,BaseManager manager){
		this(context,-1,manager);
	}

	/**
	 * 在子类中onItemClick等使用
	 * @author wang wei
	 */
	public boolean notAnimition(){
		hideInput();
		if(Math.abs(System.currentTimeMillis()-l)>500){
			l=System.currentTimeMillis();
			return Application.dcEngine.notAnimition();
		}else{
			return false;
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		System.gc();
		hideInput();
		return super.onTouchEvent(event);
	}
	/**
	 * 在动画期间拦截click事件
	 */
	public void onClick(View v){
		if(notAnimition()){
			onClicked(v);
		}
	}
	/**
	 * 响应各种点击事件,默认调用manager.onClicked(v.getId())进行处理,可以在子类中重写
	 */
	public void onClicked(View v){
		manager.onClicked(v.getId());
	}
	/**
	 * 无论点击进入,或者别的DC后退进入此DC,都会触发此方法,不要放置费时的动作
	 * @author wang wei
	 */
	public void onShow(){
		requestFocus();
		setEnabled(true);
		setClickable(true);
	}
	
	/**
	 * 设置是否被显示
	 * @author wang wei
	 */
	public void setShown(boolean isShown){	
		mIsShown=isShown;
	}
	/**
	 * 当前页面是否被显示
	 * @author wang wei
	 */
	public boolean isShown(){
		return mIsShown;
	}
	public void hideInput(){
		inputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
	}
}
